package com.bubblegumproject.ogo.swing.validation;

import javax.swing.text.JTextComponent;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * Ensure a JTextComponent contains a valid URL.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class ValidUrlValidator extends AbstractValidator {

    public ValidUrlValidator(JTextComponent component) {
        setTarget(component);
    }

    public boolean isValid() {
        try {
            URL url = new URL(((JTextComponent) getTarget()).getText().trim());
            return true;
        } catch (MalformedURLException mue) {
            return false;
        }
    }

    public static ValidUrlValidator ValidUrl(JTextComponent component) {
        return new ValidUrlValidator(component);
    }
}
